package Service;
import Service.Security;
import facecat.topin.core.*;
import java.util.*;

public class SecurityService {
    public ArrayList<Security> m_securities = new ArrayList<Security>();

    public HashMap<String, Security> m_securitiesMap = new HashMap<String, Security>();

    private boolean m_loaded;

    /*
     * 获取是否已加载
     */
    public boolean isLoaded()
    {
        return m_loaded; 
    }

    /*
     * 根据代码获取证券
     *@param code 证券代码
     *@param security 证券信息
     */
    public boolean getSecurityByCode(String code, Security security)
    {
        if (!m_loaded)
        {
            return false;
        }
        String key = code.toUpperCase();
        key = key.replace("\r", "");
        key = key.replace("\n", "");
        if (m_securitiesMap.containsKey(code))
        {
            Security copySecurity = m_securitiesMap.get(code);
            security.m_code = copySecurity.m_code;
            security.m_name = copySecurity.m_name;
            security.m_type = copySecurity.m_type;
            security.m_block = copySecurity.m_block;
            security.m_pingyin = copySecurity.m_pingyin;
            return true;
        }
        else
        {
            return false;
        }
    }

    /*
     * 加载证券
     */
    public void load(ArrayList<Security> securitiesCache)
    {
        m_securities.clear();
        m_securitiesMap.clear();
        int securitiesSize = securitiesCache.size();
        for (int i = 0; i < securitiesSize; i++)
        {
            Security security = securitiesCache.get(i);
            m_securitiesMap.put(security.m_code.toUpperCase(), security);
            m_securities.add(security);
        }
        securitiesCache.clear();
        m_loaded = true;
    }
}
